package com.css.fxfzxczh.modules.disasterSituationDataManage.houMonData.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.css.fxfzxczh.base.dict.repository.entity.DictItemEntity;
import com.css.fxfzxczh.base.dict.service.DictService;
import com.css.fxfzxczh.modules.disasterSituationDataManage.houMonData.entity.HouMonData;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.util.UUIDGenerator;
import com.css.fxfzxczh.zcpt.sys.service.SysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Component
public class HouMonExcelVerifyHandler implements IExcelVerifyHandler<HouMonData> {

    @Autowired
    SysAreaService sysAreaService;
    @Autowired
    DictService dictService;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(HouMonData houMonData) {

        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        StringBuilder message = new StringBuilder("");

        if (!PlatformObjectUtils.isEmpty(houMonData.getProvince()) && !PlatformObjectUtils.isEmpty(houMonData.getCity())  && !PlatformObjectUtils.isEmpty(houMonData.getArea())) {
            String divisionIds = null;
            try {
                divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(houMonData.getProvince(),"utf-8"),URLEncoder.encode(houMonData.getCity(),"utf-8"),URLEncoder.encode(houMonData.getArea(),"utf-8"));
            } catch (Exception e){

            }
            if (PlatformObjectUtils.isEmpty(divisionIds)) {
                result.setSuccess(false);
                message.append("省市区县名称 不符合规范！");
            }
        }else{
            if (PlatformObjectUtils.isEmpty(houMonData.getProvince())) {
                result.setSuccess(false);
                message.append("省 不能为空！");
            }
            if (PlatformObjectUtils.isEmpty(houMonData.getCity())) {
                result.setSuccess(false);
                message.append("市 不能为空！");
            }
            if (PlatformObjectUtils.isEmpty(houMonData.getArea())) {
                result.setSuccess(false);
                message.append("区(县) 不能为空！");
            }
        }

        if (PlatformObjectUtils.isEmpty(houMonData.getAddress())) {
            result.setSuccess(false);
            message.append("详细地址 不能为空！");
        }
        if (PlatformObjectUtils.isEmpty(houMonData.getSamplingPointName())) {
            result.setSuccess(false);
            message.append("抽样点名称 不能为空！");
        }
        if (PlatformObjectUtils.isEmpty(houMonData.getName())) {
            result.setSuccess(false);
            message.append("房屋单体名称 不能为空！");
        }


        if (PlatformObjectUtils.isEmpty(houMonData.getLongitude())) {
            result.setSuccess(false);
            message.append("经度 不能为空！");
        }
        if (PlatformObjectUtils.isEmpty(houMonData.getLatitude())) {
            result.setSuccess(false);
            message.append("纬度 不能为空！");
        }
        if (PlatformObjectUtils.isEmpty(houMonData.getStructureType())) {
            result.setSuccess(false);
            message.append("结构类型 不能为空！");
        }else{
            //校验导入的数据是否规范,导入的值数据字典中是否存在
            String structureType = houMonData.getStructureType();
            List<String> list = checkData("jglx");
            if(!list.contains(structureType)){
                result.setSuccess(false);
                message.append("结构类型 不正确！");
            }
        }

        if (PlatformObjectUtils.isEmpty(houMonData.getDestructionType())) {
            result.setSuccess(false);
            message.append("破坏情况 不能为空！");
        }else{
            String destructionType = houMonData.getDestructionType();
            List<String> list = checkData("phlx");
            if(!list.contains(destructionType)){
                result.setSuccess(false);
                message.append("破坏情况 不正确！");
            }
        }
        if (PlatformObjectUtils.isEmpty(houMonData.getFloorage())) {
            result.setSuccess(false);
            message.append("建筑面积（平方米） 不能为空！");
        }
        if (!PlatformObjectUtils.isEmpty(houMonData.getPurposeType())) {
            List<String> list = checkData("ytlx");
            if(!list.contains(houMonData.getPurposeType())){
                result.setSuccess(false);
                message.append("用途类型 不正确！");
            }
        }
        result.setMsg(message.toString());
        return result;
    }

    private List<String> checkData(String code){
        List<DictItemEntity> validDictItemsByDictCode = dictService.getValidDictItemsByDictCode(code);
        List<String> list = new ArrayList<>();
        for(DictItemEntity dictItemEntity: validDictItemsByDictCode){
            String dictItemCode = dictItemEntity.getDictItemCode();
            list.add(dictItemCode);
        }
        return list;
    }


}
